﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HomeworkApp
{
    /// <summary>
    /// Interaction logic for TaskBox.xaml
    /// </summary>
    public enum TaskStatus { Empty, NotFinished, Finished }

    public partial class TaskBox : UserControl
    {
        public TaskBox()
        {
            InitializeComponent();
            HideCommentBox();
            this.showTaskInReport = true;
        }

        private bool showTaskInReport;
        private MainWindow containerWindow;

        public bool ShowTaskInReport
        {
            get
            {
                return this.showTaskInReport;
            }
            set
            {
                if (value == true)
                {
                    Image icon = this.ChangeVisibility_Icon as Image;
                    icon.Source = new BitmapImage(new Uri("pack://application:,,,/Images/visible.png"));
                    this.ActivateTask();
                }
                else
                {
                    Image icon = this.ChangeVisibility_Icon as Image;
                    icon.Source = new BitmapImage(new Uri("pack://application:,,,/Images/notvisible.png"));
                    this.DeactivateTask();
                }
                this.showTaskInReport = value;
                ColorTaskBoxByType();
            }
        }

        private void DeactivateTask()
        {
            this.HideCommentBox();
            this.Task_MainControls.Visibility = Visibility.Hidden;
        }

        private void ActivateTask()
        {            
            //Task_MainControls            
            this.Task_MainControls.Visibility = Visibility.Visible;
            if (this.ToggleComment.IsChecked==true)
            {
                this.ShowCommentBox();
            }
        }

        private TaskStatus taskStatus;

        public TaskStatus Status
        {
            get
            {
                return this.taskStatus;
            }
            set
            {
                this.taskStatus = value;
                this.ColorTaskBoxByType();
            }
        }

        public string TaskName
        {
            get
            {
                return this.TaskId_Label.Content.ToString();
            }
            set
            {
                this.TaskId_Label.Content = value;

            }
        }

        public string Comment
        {
            get
            {
                return this.TaskComment_TextBlock.Text;
            }
            set
            {
                this.TaskComment_TextBlock.Text = value;
            }
        }

        private void ColorTaskBoxByType()
        {
            SolidColorBrush background = null;
            //SolidColorBrush foreground = null;
            SolidColorBrush border = null;
            if (!this.showTaskInReport)
            {
                background = new SolidColorBrush(Color.FromRgb(250, 250, 250));
                //foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
                border = new SolidColorBrush(Color.FromRgb(170, 170, 170));
            }
            else
            {
                if (this.taskStatus == TaskStatus.Empty)
                {
                    background = new SolidColorBrush(Color.FromRgb(255, 240, 240));
                    //foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
                    border = new SolidColorBrush(Color.FromRgb(255, 0, 0));
                }
                else if (this.taskStatus == TaskStatus.NotFinished)
                {
                    background = new SolidColorBrush(Color.FromRgb(255, 243, 203));
                    //foreground = new SolidColorBrush(Color.FromRgb(255, 204, 0));
                    border = new SolidColorBrush(Color.FromRgb(255, 204, 0));
                }
                else if (this.taskStatus == TaskStatus.Finished)
                {
                    background = new SolidColorBrush(Color.FromRgb(240, 255, 240));
                    //foreground = new SolidColorBrush(Color.FromRgb(0, 255, 0));
                    border = new SolidColorBrush(Color.FromRgb(0, 255, 0));
                }
            }
            this.StatusIndicator_Box.Fill = background;
            this.StatusIndicator_Box.Stroke = border;            
        }

        public TaskBox(string taskName,MainWindow containerWindow)
            : this()
        {
            this.TaskName = taskName;
            this.Status = TaskStatus.Empty;
            this.containerWindow = containerWindow;
        }

        private void MouseOver(object sender, MouseEventArgs e)
        {
            Control uielement = sender as Control;
            if (uielement != null)
            {
                this.containerWindow.ToolTip_Bar.Content = uielement.ToolTip.ToString();
            }
        }

        private void MouseOut(object sender, MouseEventArgs e)
        {
            this.containerWindow.ToolTip_Bar.Content = "";
        }

        private void TaskStatus_Changed(object sender, SelectionChangedEventArgs e)
        {
            int selI = this.TaskStatus_Combo.SelectedIndex;
            switch (selI)
            {
                case 1:
                    this.Status = TaskStatus.NotFinished;
                    break;
                case 2:
                    this.Status = TaskStatus.Finished;
                    break;
                default:
                    this.Status = TaskStatus.Empty;
                    break;
            }
        }

        private void HideCommentBox()
        {
            this.TaskComment_TextBlock.Visibility = Visibility.Collapsed;
            this.CommentLabel.Visibility = Visibility.Collapsed;
            this.TaskComment_TextBlock.Text = "";
        }

        private void ShowCommentBox()
        {
            this.CommentLabel.Visibility = Visibility.Visible;
            this.TaskComment_TextBlock.Visibility = Visibility.Visible;
        }

        private void ToggleComment_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checker = sender as CheckBox;
            if (checker.IsChecked == false)
            {
                this.HideCommentBox();
            }
            else
            {
                this.ShowCommentBox();
            }
        }

        private void ChangeVisibility_Click(object sender, RoutedEventArgs e)
        {
            this.ShowTaskInReport = !this.showTaskInReport;
        }
    }
}
